<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="饭店名称" prop="hotelName">
        <el-input
          v-model="queryParams.resName"
          placeholder="请输入饭店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="handTime">
        <el-date-picker
          v-model="queryParams.handTime"
          clearable
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="warning" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="invoiceList">
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="配送日期" align="center" prop="ydDateInfo" min-width="190" />
      <el-table-column label="饭店名称" align="center" prop="resName" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="食品名称" align="center" prop="foodName" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="餐别" align="center" prop="categoryStr" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="配送时间" align="center" prop="psStart" min-width="120">
        <template #default="scope">
          {{ scope.row.psStart }} 至 {{ scope.row.psEnd }}
        </template>
      </el-table-column>
      <el-table-column label="配送地址" align="center" prop="address" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="预定份数" align="center" prop="num" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系方式" align="center" prop="phone" min-width="110" />
      <el-table-column label="金额" align="center" prop="total" />
      <el-table-column label="备注" align="center" prop="remarks" min-width="150" :show-overflow-tooltip="true" />
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { getCurrentInstance, ref, toRefs } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { getOrderFoodList } from '@/api/logex/order'
import { useRoute } from 'vue-router'
import usePermissionStore from '@/store/modules/permission'


const { proxy } = getCurrentInstance()
const route = useRoute()
const permissionStore = usePermissionStore()

const invoiceList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
let activityName = null

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activeId: undefined,
    resName: undefined,
    handTime: undefined,
    handTimeRange: undefined
  }
})

const { queryParams } = toRefs(data)

onMounted(() => {
  permissionStore.setActiveMenu('/logistics/match')
})

/** 查询发票列表 */
function getList() {
  loading.value = true
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  } else {
    queryParams.value.handTimeRange = null
  }

  queryParams.value.activeId = route.params.matchId

  getOrderFoodList(queryParams.value).then(response => {
    invoiceList.value = response.rows
    total.value = response.total
    loading.value = false

    if (invoiceList.value.length > 0) {
      activityName = invoiceList.value[0].acvName
    }
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleExport() {
  if (queryParams.value.handTime) {
    queryParams.value.handTimeRange = queryParams.value.handTime.join(',')
  }

  proxy.download('/logex/orderFood/export', {
    ...queryParams.value
  }, `${activityName}_就餐记录_${new Date().getTime()}.xlsx`)
}

defineExpose({
  init: getList
})
</script>
